//
//  ViewController.m
//  Demo
//
//

#import "ViewController.h"
#import "InputViewController.h"
#import "GoodBtn.h"
#import "GameViewController.h"
#import "NewDevice1ViewController.h"
#import "QuestionViewController.h"

@implementation ViewController

-(void)btnPressed1:(UIButton*)btn{
    GameViewController *gameView = [[GameViewController alloc] initWithNibName:@"InputViewController" bundle:nil];
    gameView.type = btn.tag;
    [self.navigationController pushViewController:gameView animated:YES];
}

- (IBAction)edit:(id)sender {
    InputViewController *input = [[InputViewController alloc] initWithNibName:@"InputViewController" bundle:nil];
    [self presentModalViewController:input animated:YES];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    if (!self.navigationController.presentedViewController) {
        [self.navigationController setNavigationBarHidden:NO animated:animated];
    }
}
/*
 - (UIStatusBarStyle)preferredStatusBarStyle
 {
 return UIStatusBarStyleLightContent;
 }
 - (BOOL)prefersStatusBarHidden
 {
 return NO;
 }*/

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    /*
     UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
     imageView.image = [IMG(@"background.png") stretchableImageWithLeftCapWidth:160 topCapHeight:460];
     [self.view addSubview:imageView];
     [imageView release];*/
    
    int font_size = 25;
    
    GoodLabel *descLabel1 = [GoodLabel labelWithFont:font_size title:@"欢迎使用"];
    descLabel1.textColor = [UIColor blackColor];
    descLabel1.frame = CGRectMake(0, 20, kScreenWidth, 30);
    descLabel1.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:descLabel1];
    
    UIImageView *logo = [[UIImageView alloc] initWithImage:IMG(@"company_logo.png")];
    logo.frame = CGRectMake(SCREEN_WIDTH/2-FW(logo)/5, FY(descLabel1)+50, FW(logo)/2.5, FH(logo)/2.5);
    [self.view addSubview:logo];
    logo.hidden = YES;
#ifndef HAIDIAN_VERSION
    logo.hidden = NO;
#endif

    GoodLabel *descLabel2 = [GoodLabel labelWithFont:font_size title:@"Wi-Fi智能家居系列"];
    descLabel2.textColor = descLabel1.textColor;
    descLabel2.frame = CGRectMake(0, FY(logo)+FH(logo)+15, kScreenWidth, 50);
    descLabel2.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:descLabel2];
    
    
//    for (int i = 0; i < 3; i++) {
//        UIImage *image = nil;
//        if (i == 0) {
//            image = IMG(@"yx.png");
//            GoodLabel *descLabel = [GoodLabel labelWithFont:13 title:@"游戏"];
//            descLabel.frame = CGRectMake(63, 215+37, 40, 20);
//            [self.view addSubview:descLabel];
//        }else if (i == 1) {
//            image = IMG(@"nwes.png");
//            GoodLabel *descLabel = [GoodLabel labelWithFont:13 title:@"新闻"];
//            descLabel.frame = CGRectMake(63+90, 215+37, 40, 20);
//            [self.view addSubview:descLabel];
//        }else if (i == 2) {
//            image = IMG(@"sh.png");
//            GoodLabel *descLabel = [GoodLabel labelWithFont:13 title:@"生活服务"];
//            descLabel.frame = CGRectMake(45+90*2, 215+37, 70, 20);
//            [self.view addSubview:descLabel];
//        }
//        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(50+i*90, 215, image.size.width/2, image.size.height/2)];
//        if (i == 1) {
//            btn.center = CGPointMake(btn.center.x+5, btn.center.y);
//        }
//        btn.tag = i;
//        [btn setBackgroundImage:image forState:UIControlStateNormal];
//        [btn addTarget:self action:@selector(btnPressed1:) forControlEvents:UIControlEventTouchUpInside];
//        [self.view addSubview:btn];
//    }
    
    int y = kScreenHeight-160;
    int left =20;
    int scale = 3;
    for (int i = 0; i < 3; i++) {
        
        UIImage *image = nil;
        if (i == 0) {
            image = IMG(@"chinese.png");
        }else if (i == 1) {
            image = IMG(@"english.png");
        }else if (i == 2) {
            image = IMG(@"german.png");
        }
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(left+i*100, y-5, image.size.width/scale, image.size.height/scale)];
//        btn.imageEdgeInsets = UIEdgeInsetsMake(5, 0, 5, btn.frame.size.width-image.size.width);
//        if (i == 2) {
//            btn.center = CGPointMake(btn.center.x, btn.center.y-1);
//        }
        btn.tag = i;
        [btn setImage:image forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
//        if (i == 0) {
//            GoodLabel *descLabel = [GoodLabel labelWithFont:13 title:@"中文"];
//            descLabel.frame = CGRectMake(btn.frame.origin.x+btn.frame.size.width+4-50, y-1, 70, 16);
//            [self.view addSubview:descLabel];
//        }else if (i == 1) {
//            GoodLabel *descLabel = [GoodLabel labelWithFont:13 title:@"English"];
//            descLabel.frame = CGRectMake(btn.frame.origin.x+btn.frame.size.width+4-50, y-1, 70, 16);
//            [self.view addSubview:descLabel];
//        }else if (i == 2) {
//            GoodLabel *descLabel = [GoodLabel labelWithFont:13 title:@"German"];
//            descLabel.frame = CGRectMake(btn.frame.origin.x+btn.frame.size.width+4-50, y-1, 70, 16);
//            [self.view addSubview:descLabel];
//        }
    }
    
    y = y+45;
    for (int i = 0; i < 3; i++) {
        UIImage *image = nil;
        if (i == 0) {
            image = IMG(@"francaise.png");
        }else if (i == 1) {
            image = IMG(@"espanola.png");
        }else if (i == 2) {
            image = IMG(@"italiana.png");
        }
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(left+i*100, y-5, image.size.width/scale, image.size.height/scale)];
//        btn.imageEdgeInsets = UIEdgeInsetsMake(5, 0, 5, btn.frame.size.width-image.size.width);
//        if (i == 2) {
//            btn.center = CGPointMake(btn.center.x, btn.center.y-1);
//        }
        btn.tag = 3+i;
        [btn setImage:image forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
//        if (i == 0) {
//            GoodLabel *descLabel = [GoodLabel labelWithFont:13 title:@"française"];
//            descLabel.frame = CGRectMake(btn.frame.origin.x+btn.frame.size.width+4-50, y-1, 70, 16);
//            [self.view addSubview:descLabel];
//        }else if (i == 1) {
//            GoodLabel *descLabel = [GoodLabel labelWithFont:13 title:@"española"];
//            descLabel.frame = CGRectMake(btn.frame.origin.x+btn.frame.size.width+4-50, y-1, 70, 16);
//            [self.view addSubview:descLabel];
//        }else if (i == 2) {
//            GoodLabel *descLabel = [GoodLabel labelWithFont:13 title:@"Italiana"];
//            descLabel.frame = CGRectMake(btn.frame.origin.x+btn.frame.size.width+4-50, y-1, 70, 16);
//            [self.view addSubview:descLabel];
//        }
    }
    
    GoodLabel *descLabelLeft = [GoodLabel labelWithFont:font_size-5 title:STR(@"人物互联·随心掌控")];
    descLabelLeft.textColor = [UIColor blackColor];
    descLabelLeft.textAlignment = UITextAlignmentCenter;
    descLabelLeft.frame = CGRectMake(0, kScreenHeight-60, kScreenWidth, 30);
    [self.view addSubview:descLabelLeft];
    
  
//    UIImageView *imageViewDot = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth/2-5, kScreenHeight-48, 10, 9)];
//    imageViewDot.image = IMG(@"dot.png");
//    [self.view addSubview:imageViewDot];
    
//    NSArray *arraya = nil;
//    NSString *path = [NSDataInfo storePathWithFileName:@"TV.dat"];
//    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
//        arraya =  [NSDataInfo unarchiverObjectFromPath:path key:@"TV"];
//        for (NSDataInfo *dataInfo in arraya) {
//            NSLog(@"dataInfo %@ %d ", dataInfo.app_cmd, dataInfo.isStudy);
//        }
//    }
//    NSDataInfo *dataInfo = [arraya objectAtIndex:4];
//    dataInfo.isStudy = YES;
//    for (NSDataInfo *dataInfo in arraya) {
//        NSLog(@"dataInfo %@ %d ", dataInfo.app_cmd, dataInfo.isStudy);
//    }
//    //
//    //    NSMutableArray *array = [[NSMutableArray alloc] init];
//    //    for (int i=0; i<10; i++) {
//    //        NSDataInfo *dataInfo = [[NSDataInfo alloc] init];
//    //        dataInfo.app_cmd = [NSString stringWithFormat:@"%d", i+1];
//    //        [array addObject:dataInfo];
//    //    }
//    path = [NSDataInfo storePathWithFileName:@"TV.dat"];
//    [NSDataInfo archiverObject:arraya path:path key:@"TV"];
    
	// Do any additional setup after loading the view, typically from a nib.
    
    
    
    NSDictionary *oldList = [UD objectForKey:LANIPAddress_list];
    NSMutableDictionary *dicList = [[NSMutableDictionary alloc] initWithDictionary:oldList];
    for (int i=0; i<dicList.count; i++) {
        NSString *key = [[dicList allKeys] objectAtIndex:i];
        NSString *value = [[dicList allValues] objectAtIndex:i];
        if ([key hasPrefix:@"R"]) {
            [dicList removeObjectForKey:key];
            key = [key substringFromIndex:1];
            [dicList setValue:value forKey:key];
        }
    }
    [UD setObject:dicList forKey:LANIPAddress_list];
    [UD synchronize];
    NSDictionary *oldListSocket = [UD objectForKey:LAN_SOCKET_IPAddress_list];
    NSMutableDictionary *dicList1 = [[NSMutableDictionary alloc] initWithDictionary:oldListSocket];
    for (int i=0; i<dicList1.count; i++) {
        NSString *key = [[dicList1 allKeys] objectAtIndex:i];
        NSString *keyValue = [[dicList1 allValues] objectAtIndex:i];
        if ([key hasPrefix:@"S"]) {
            [dicList1 removeObjectForKey:key];
            key = [key substringFromIndex:1];
            [dicList1 setValue:keyValue forKey:key];
        }
    }
    [UD setObject:dicList1 forKey:LAN_SOCKET_IPAddress_list];
    [UD synchronize];
    
    [self performSelector:@selector(autoEnterNextView)];
}
-(NSString*)systemLanguage {
    return [[NSLocale preferredLanguages] objectAtIndex:0];
}

-(void)autoEnterNextView{
    NSString *language = [self systemLanguage];
    if ([language isEqualToString:@"zh-Hans"]) {
        [UD setObject:CH forKey:CUR_LANGUAGE];
        [UD synchronize];
    }else{
        [UD setObject:EN forKey:CUR_LANGUAGE];
        [UD synchronize];
    }
    [self enterNextView];
}
-(void)enterNextView{
    InputViewController *input = [[InputViewController alloc] initWithNibName:@"InputViewController" bundle:nil];
    [self.navigationController pushViewController:input animated:YES];
}

-(void)btnPressed:(UIButton*)btn{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(autoEnterNextView) object:nil];
    
    if (btn.tag == 0) {
        [UD setObject:CH forKey:CUR_LANGUAGE];
        [UD synchronize];
    }else if (btn.tag == 1) {
        [UD setObject:EN forKey:CUR_LANGUAGE];
        [UD synchronize];
    }else {
        [UD setObject:CH forKey:CUR_LANGUAGE];
        [UD synchronize];
    }
    [self enterNextView];
}


- (void)viewDidUnload {
    [self setResult:nil];
    [super viewDidUnload];
}
@end
